package com.matchmealpha;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class RecentView extends LinearLayout implements OnClickListener {

	private TextView date;
	private TextView user1;
	private TextView location1;
	private TextView text1;

	private TextView user2;
	private TextView location2;
	private TextView text2;

	private ImageView pic;
	private TextView tagText;
	private TextView likeText;
	private ListView commentList;

	private Button moreButton;
	private Button likeButton;
	private Button commentButton;

	private int callerID;
	private Context context;

	public RecentView(Context context, Post post, int callerID) {
		super(context);
		this.callerID = callerID;
		this.context = context;
		initLayout();
		setRecentItem(post);
		initListener();
	}

	public void setRecentItem(Post post) {
		date.setText(post.getTime1()); // TODO calculate the time1 from now
		text1.setText(post.getDescription1());
		text2.setText(post.getDescription2());
		tagText.setText(post.getTagList().toString());
		likeText.setText(post.getLikeList().toString());
		user1.setText(post.getUserID1());
		user2.setText(post.getUserID2());
		location1.setText(post.getLocation1());
		location2.setText(post.getLocation2());
		pic.setImageBitmap(post.getBitMapImage());
		//bm.recycle();
		// TODO pic, like list, comment list

	}

	private void initLayout() {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.recent_item_view, this, true);

		user1 = (TextView) this.findViewById(R.id.RecentItemUser1Text);
		location1 = (TextView) this.findViewById(R.id.RecentItemLocation1Text);
		text1 = (TextView) this.findViewById(R.id.RecentItemText1Text);

		user2 = (TextView) this.findViewById(R.id.RecentItemUser2Text);
		location2 = (TextView) this.findViewById(R.id.RecentItemLocation2Text);
		text2 = (TextView) this.findViewById(R.id.RecentItemText2Text);

		date = (TextView) this.findViewById(R.id.RecentItemDateText);
		pic = (ImageView) this.findViewById(R.id.RecentItemPicImage);
		likeText = (TextView) this.findViewById(R.id.RecentItemLikeText);
		tagText = (TextView) this.findViewById(R.id.RecentItemTagText);
		commentList = (ListView) this.findViewById(R.id.RecentItemCommentList);

		moreButton = (Button) this.findViewById(R.id.RecentItemMoreButton);
		likeButton = (Button) this.findViewById(R.id.RecentItemLikeButton);
		commentButton = (Button) this
				.findViewById(R.id.RecentItemCommentButton);

	}

	private void initListener() {
		// TODO commentList
		// TODO tag list
		// TODO like list
		user1.setOnClickListener(this);
		user2.setOnClickListener(this);
		// location1.setOnClickListener(this);
		// location2.setOnClickListener(this);
		moreButton.setOnClickListener(this);
		likeButton.setOnClickListener(this);
		commentButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case (R.id.RecentItemUser1Text):
			Intent intent1 = new Intent(this.context, TimelineActivity.class);
			intent1.putExtra("back", true);
			intent1.putExtra("callerID", this.callerID);
			View view1 = MatchMeTabWidget.getCurrGroup(this.callerID)
					.getLocalActivityManager().startActivity(
							"TimelineActivity",
							intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
					.getDecorView();
			MatchMeTabWidget.myReplaceView(view1, this.callerID);
			break;
		case (R.id.RecentItemUser2Text):
			Intent intent2 = new Intent(this.context, TimelineActivity.class);
			intent2.putExtra("back", true);
			intent2.putExtra("callerID", this.callerID);
			View view2 = MatchMeTabWidget.getCurrGroup(this.callerID)
					.getLocalActivityManager().startActivity(
							"TimelineActivity",
							intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
					.getDecorView();
			MatchMeTabWidget.myReplaceView(view2, this.callerID);
			break;
		case (R.id.RecentItemPicImage):
			Intent intent3 = new Intent(this.context, PostActivity.class);
			intent3.putExtra("callerID", this.callerID);
			View view3 = MatchMeTabWidget.getCurrGroup(this.callerID)
					.getLocalActivityManager().startActivity("PostActivity",
							intent3.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
					.getDecorView();
			MatchMeTabWidget.myReplaceView(view3, this.callerID);
			break;
		case (R.id.RecentItemMoreButton):
			break;
		case (R.id.RecentItemLikeButton):
			break;
		case (R.id.RecentItemCommentButton):
			final AlertDialog.Builder alert = new AlertDialog.Builder(
					this.context);
			final EditText input = new EditText(this.context);
			alert.setView(input);
			alert.setMessage("Enter Comment");
			alert.setPositiveButton("Ok",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							String value = input.getText().toString();
							// Do something with value!
						}
					});
			alert.setNegativeButton("Cancel",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
						}
					});
			alert.show();
			break;
		}

	}
}
